//
//  CXBaseViewController.m
//  Cxzc
//
//  Created by 张露杰 on 17/8/7.
//  Copyright © 2017年 张露杰. All rights reserved.
//


#import "HWDBaseViewController.h"
@interface HWDBaseViewController ()<UISearchBarDelegate>
@end
@implementation HWDBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Navi"] forBarMetrics:UIBarMetricsDefault];
    self.view.backgroundColor = backColor;
    [self setupBackBUtton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Configurations

- (void)setupBackBUtton {
    if (!self.navigationController) {
        return;
    }
    if (self != self.navigationController.viewControllers[0] && [self needShowBackButton]) {
        [self initBackButton];
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}

- (BOOL)needShowBackButton {
    return YES;
}

- (BOOL)hidesBottomBarWhenPushed {
    if (self != self.navigationController.viewControllers[0]) {
        return YES;
    }
    return NO;
}

- (CGSize)imageItemSize {
    return CGSizeMake(44,44);
}

/**
 *  计算导航栏左右两边item的宽度
 */
- (CGFloat)itemViewWidth {
    CGRect leftItemBounds = self.navigationItem.leftBarButtonItem.customView.bounds;
    // 这一步转换是为了得到barbutton在navigationbar上的frame
    CGRect convertFrame = [self.navigationItem.leftBarButtonItem.customView convertRect:leftItemBounds toView:self.navigationController.navigationBar];
    // Note: 这个padding的值就是barbutton与导航栏左侧的边距，6plus上这个值是20，4~6都是16，6plus上得到的值是其它小尺寸设备的1.25倍。这里按标准边距来处理
    CGFloat padding = convertFrame.origin.x;
    
    CGRect rightItemBounds = self.navigationItem.rightBarButtonItem.customView.bounds;
    CGFloat width = MAX(leftItemBounds.size.width, rightItemBounds.size.width) + padding;
    return width;
}


#pragma mark - Init Views

-(void)initTitleWithTitle:(NSString *)title
{
    UILabel *titleLabel = [TomamaUI createLabel:CGRectMake(0, 0, [TomamaUI getStringWeithtWith:title andHeigth:20 andFont:[UIFont systemFontOfSize:19*screenW_6]], 20) textAligment:1 font:[UIFont systemFontOfSize:19*screenW_6] textColor:[UIColor whiteColor] text:title];
    self.navigationItem.titleView = titleLabel;
    
}

- (void)initBackButton {
    [self initLeftBarButtonWithImage:@"back"];
}

- (void)initLeftBarButtonWithImage:(NSString *)imageName {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button addTarget:self action:@selector(onTapLeftBarButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonitem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = barButtonitem;
}

- (void)initRightBarButtonWithImage:(NSString *)imageName {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button addTarget:self action:@selector(onTapRightBarButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonitem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = barButtonitem;
}

- (void)initLeftBarButtonWithTitle:(NSString *)title {
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:title
                                                                  style:UIBarButtonItemStylePlain target:self
                                                                 action:@selector(onTapLeftBarButton)];
    self.navigationItem.leftBarButtonItem = barButton;
}

- (void)initRightBarButtonWithTitle:(NSString *)title andTitleColor:(UIColor *)titleColor andFont:(CGFloat)font{
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:title
                                                                  style:UIBarButtonItemStylePlain target:self
                                                                 action:@selector(onTapRightBarButton)];
    self.navigationItem.rightBarButtonItem = barButton;
    [self.navigationItem.rightBarButtonItem setTintColor:titleColor];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil] forState:UIControlStateNormal];
}

-(void)initRightBarButtonsWithImageArray:(NSArray *)array
{
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0, 0, 30, 30);
    [button1 setImage:[UIImage imageNamed:array[0]] forState:UIControlStateNormal];
    button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button1 addTarget:self action:@selector(onTapRightBarButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonitem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(0, 0, 30, 30);
    [button2 setImage:[UIImage imageNamed:array[1]] forState:UIControlStateNormal];
    button2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button2 addTarget:self action:@selector(onTapRightBarButton2) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonitem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
    
    NSArray *buttons = [NSArray arrayWithObjects:barButtonitem1,barButtonitem2, nil];
    self.navigationItem.rightBarButtonItems = buttons;
}

- (UISegmentedControl *)segmentControlWithItems:(NSArray *)items {
    
    UISegmentedControl *segmentedControl= [[UISegmentedControl alloc] initWithItems:items];
    segmentedControl.backgroundColor = greenColor;
    segmentedControl.tintColor = [UIColor whiteColor];
    segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;
//    segmentedControl.layer.borderWidth = 1.0;
    segmentedControl.layer.cornerRadius = 5.0;
    segmentedControl.layer.masksToBounds = YES;
    //设置字体
    [segmentedControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f],NSForegroundColorAttributeName:  [UIColor whiteColor]} forState:UIControlStateNormal];
    [segmentedControl setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0f], NSForegroundColorAttributeName :greenColor} forState:UIControlStateSelected];
    segmentedControl.selectedSegmentIndex = 0;
    // 设置控件选项的宽度
    NSArray *titles = [items sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"length" ascending:YES]]];
    NSString *maxWidthTitle = [titles lastObject];
    CGSize textSize = [maxWidthTitle sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]}];
    CGFloat itemWidth = textSize.width + 35;
    for (int i = 0 ; i < items.count ; i ++) {
        [segmentedControl setWidth:itemWidth forSegmentAtIndex:i];
    }
     segmentedControl.frame = CGRectMake(segmentedControl.frame.origin.x,segmentedControl.frame.origin.y-10, segmentedControl.frame.size.width,30);
    [segmentedControl addTarget:self action:@selector(segmentControlClicked:) forControlEvents:UIControlEventValueChanged];
    return segmentedControl;
}

-(void)initSearchBarWithPlaceholder:(NSString *)placeholder showsCancelButton:(BOOL)showsCancelButton
{
    if (showsCancelButton) {
        self.navigationItem.leftBarButtonItem = nil;
    }
    
    self.navigationItem.hidesBackButton = YES;
    
    _baseSesearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 44)];
    _baseSesearchBar.delegate = self;
    _baseSesearchBar.showsCancelButton = showsCancelButton;
    _baseSesearchBar.placeholder = placeholder;
    if (@available(iOS 11.0, *)) {
        [[_baseSesearchBar.heightAnchor constraintEqualToConstant:44.0] setActive:YES];
    }
//    UIImage* searchBarBg = [TomamaUI GetImageWithColor:[UIColor clearColor] andHeight:40.0f];
    //设置背景图片
//    [_baseSesearchBar setBackgroundImage:searchBarBg];
    //设置背景色
    [_baseSesearchBar setBackgroundColor:[UIColor clearColor]];
    
    UIButton *btn = [_baseSesearchBar valueForKey:@"_cancelButton"];
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    btn.enabled = YES;
    [btn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];

    UITextField *searchField=[_baseSesearchBar valueForKey:@"_searchField"];
    searchField.borderStyle = UITextBorderStyleNone;
    searchField.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
    searchField.layer.cornerRadius = 5.0;
    searchField.layer.masksToBounds = YES;
    searchField.tag = 100;
    searchField.clearButtonMode = UITextFieldViewModeNever;
    
    for (UIGestureRecognizer *tap in _baseSesearchBar.gestureRecognizers) {
        [_baseSesearchBar removeGestureRecognizer:tap];
    }
    self.navigationItem.titleView = _baseSesearchBar;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(SCREENW - 65, 0, 50, 44);
    [button setTitle:@"取消" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:17];
    button.enabled = YES;
    [button addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    UIBarButtonItem *barButtonitem = [[UIBarButtonItem alloc] initWithCustomView:button];
//    self.navigationItem.rightBarButtonItem = barButtonitem;
    [_baseSesearchBar addSubview:button];
}

- (void)adressViewWith:(UIView *)view
{
    self.navigationItem.titleView = view;
}
#pragma mark - Event

- (void)onTapLeftBarButton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)cancel{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onTapRightBarButton {
    
}

- (void)onTapRightBarButton2 {
    
}

- (void)segmentControlClicked:(UISegmentedControl *)segmentControl {
    
}

- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

- (void)dealloc {
    NSLog(@"销毁");
}

@end
